package com.nmgdz.bigdata.controller;

import com.nmgdz.bigdata.entity.User;
import com.nmgdz.bigdata.service.RedisService;
import com.nmgdz.bigdata.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    //查询
    @GetMapping("/queryById")
    public String queryById(int id) {
        //从redis获取key为Id User
        String userStr = redisService.get(id + "");
        //判断是否存在
        if (userStr != null) {
            //查询 设置到redis
            redisService.set(id + "", userService.queryById(id).toString());
        }
        return userStr;
    }


    //增 改
    @PostMapping("/saveOrUpdate")
    public User saveOrUpdateUser(User user) {
        return userService.save(user);
    }

    //删除用户
    @DeleteMapping("/delete")
    public Boolean deleteUser(int id) {
        userService.deletUserById(id);
        return true;
    }

}
